package com.android.kotlincoroutinemvvmdemo.view

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import android.view.animation.LinearInterpolator
import com.android.kotlincoroutinemvvmdemo.R

class AnimationView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : View(context,attributeSet) {

    private var mWidth = 0
    private var mHeight = 0

    private val floatArray = floatArrayOf(30F, 150F, 60F, 120F)

    private var mStartAngle = -90F

    private var mIndex = 0

    private var swipeAngle : Float = 0F
        set(value) {
            field = value
            invalidate()
        }

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            style = Paint.Style.FILL
            color = context.resources.getColor(R.color.color_da8989)
        }
    }

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            style = Paint.Style.FILL
            color = context.resources.getColor(R.color.color_64C630)
        }
    }

    private val mPaint2 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            style = Paint.Style.FILL
            color = context.resources.getColor(R.color.color_ff5722)
        }
    }

    private val mPaint3 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            style = Paint.Style.FILL
            color = context.resources.getColor(R.color.color_673ab7)
        }
    }

    private val mPaintArray = arrayOf(mPaint0, mPaint1, mPaint2, mPaint3)

    private var mUpdateCallback: IUpdateCallback? = null

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        mWidth = when(MeasureSpec.getMode(widthMeasureSpec)) {
            MeasureSpec.EXACTLY -> {
                MeasureSpec.getSize(widthMeasureSpec)
            }
            else -> {
                context.resources.getDimension(R.dimen.dp_100).toInt()
            }
        } - paddingStart - paddingEnd

        mHeight = when(MeasureSpec.getMode(heightMeasureSpec)) {
            MeasureSpec.EXACTLY -> {
                MeasureSpec.getSize(heightMeasureSpec)
            }
            else -> {
                context.resources.getDimension(R.dimen.dp_100).toInt()
            }
        } - paddingTop - paddingBottom

        setMeasuredDimension(mWidth, mHeight)
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.apply {
            drawArc(
                0F,
                0F,
                mWidth.toFloat(),
                mHeight.toFloat(),
                mStartAngle,
                swipeAngle,
                true,
                mPaintArray[mIndex.coerceIn(0,mPaintArray.size -1)]
            )
        }
    }

    override fun dispatchDraw(canvas: Canvas?) {
        super.dispatchDraw(canvas)
        var mStartAngle = -90F
        for (i in 0.until(mIndex)) {
            canvas?.apply {
                drawArc(
                    0F,
                    0F,
                    mWidth.toFloat(),
                    mHeight.toFloat(),
                    mStartAngle,
                    floatArray[i],
                    true,
                    mPaintArray[i]
                )
                mStartAngle += floatArray[i]
            }
        }
    }

    fun startSwipe() {
        val animatorSet = AnimatorSet()
        val animatorArray = mutableListOf<Animator>()
        var startAngle = 0F
        var swipeAngle = 0F
        for (i in floatArray.indices) {

            swipeAngle += floatArray[i]
            val objectAnimator = ObjectAnimator.ofFloat(this, "swipeAngle", startAngle, swipeAngle)
            objectAnimator.addListener(object : Animator.AnimatorListener {
                override fun onAnimationStart(animation: Animator) {}
                override fun onAnimationCancel(animation: Animator) {}
                override fun onAnimationRepeat(animation: Animator) {}
                override fun onAnimationEnd(animation: Animator) {
                    //Log.d(TAG,"---onAnimationEnd---")
                    //mStartAngle += floatArray[i]
                    mIndex++
                    mUpdateCallback?.onItemAnimationFinished(mIndex)

                    if (mIndex >= floatArray.size) {
                        mUpdateCallback?.onAllAnimationFinished()
                    }
                }
            })
            //objectAnimator.start()
            animatorArray.add(objectAnimator)
            startAngle += floatArray[i]
        }

        animatorSet.playSequentially(animatorArray)
        //使用差值器避免中间会出现的停顿
        animatorSet.interpolator = LinearInterpolator()
        animatorSet.duration = 450
        animatorSet.start()
    }

    fun setUpdateCallback(mCallback: IUpdateCallback?) {
        this.mUpdateCallback = mCallback
    }

    interface IUpdateCallback {
        fun onItemAnimationFinished(itemIndex: Int)

        fun onAllAnimationFinished()
    }

    companion object {
        const val TAG = "AnimationView"
    }
}